import { Component, OnInit } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd';
import { CustomService } from 'src/app/service/custom.service';

@Component({
  selector: 'app-security',
  templateUrl: './security.component.html',
  styleUrls: ['./security.component.less']
})
export class SecurityComponent implements OnInit {

  data: any = {};
  securityGrade: string = '中等';
  securityScore: number = 30;
  isAuthShow: boolean = false;
  authMode: number = 0;
  code: string | null = null;
  codePhone: string | null = null;
  codeEmail: string | null = null;
  codeButtonText: string = '发送验证码';
  codeButtonDisabled: boolean = false;
  codeIntervalSecond: number = 120;

  showAuth(authMode: number) {
    this.code = null;
    this.isAuthShow = true;
    this.authMode = authMode;
  }

  sendCode() {
    var that = this;
    if (this.authMode === 1) {
      let testPhone = /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|16[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/;
      if (testPhone.test(this.data['phone'])) {
        this.codeIntervalSecond = 120;
        this.codeButtonDisabled = true;
        this.customService.sendPhone(this.data['phone']).subscribe((result: any) => {
          if (result['code'] === 'success') {
            this.code = result['data']['code'];
            this.codePhone = this.data['phone'];
            var a = setInterval(() => {
              that.codeIntervalSecond = that.codeIntervalSecond - 1;
              that.codeButtonText = `${that.codeIntervalSecond}秒后重发`;
              if (that.codeIntervalSecond <= 0) {
                that.codeButtonText = '发送验证码';
                this.codeButtonDisabled = false;
                window.clearInterval(a);
              }
            }, 1000);
          }
          else {
            this.codeButtonDisabled = false;
            this.modalService.error({
              nzTitle: '注册提示',
              nzContent: '发送验证码失败'
            });
          }
        });
      }
      else {
        this.modalService.error({
          nzTitle: '注册提示',
          nzContent: '手机号码输入错误'
        });
      }
    }
    else if (this.authMode === 2) {
      let testEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (testEmail.test(this.data['email'])) {
        this.codeIntervalSecond = 120;
        this.codeButtonDisabled = true;
        this.customService.sendMail(this.data['email']).subscribe((result: any) => {
          if (result['code'] === 'success') {
            this.code = result['data']['code'];
            this.codeEmail = this.data['email'];
            var a = setInterval(() => {
              that.codeIntervalSecond = that.codeIntervalSecond - 1;
              that.codeButtonText = `${that.codeIntervalSecond}秒后重发`;
              if (that.codeIntervalSecond <= 0) {
                that.codeButtonText = '发送验证码';
                this.codeButtonDisabled = false;
                window.clearInterval(a);
              }
            }, 1000);
          }
          else {
            this.codeButtonDisabled = false;
            this.modalService.error({
              nzTitle: '注册提示',
              nzContent: '发送验证码失败'
            });
          }
        });
      }
      else {
        this.modalService.error({
          nzTitle: '注册提示',
          nzContent: '电子邮箱输入错误'
        });
      }
    }
  }

  submitAuth() {
    let testPhone = /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|16[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/;
    let testEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if ((this.authMode === 1 && testPhone.test(this.data['phone'])) || this.authMode === 2 && testEmail.test(this.data['email'])) {
      if (this.code &&
        ((this.authMode === 1 && this.codePhone && this.codePhone === this.data['phone'])
          || (this.authMode === 2 && this.codeEmail && this.codeEmail === this.data['email']))
        && this.code === this.data['verifyCode']) {
        let data = {};
        if (this.authMode === 1) {
          data['phone'] = this.data['phone'];
          data['isPhoneAuth'] = true;
        }
        else if (this.authMode === 2) {
          data['email'] = this.data['email'];
          data['isEmailAuth'] = true;
        }
        console.log(data);
        this.customService.modify(data).subscribe(result => {
          if (result['code'] === 'success') {
            this.getProfile();
            this.isAuthShow = false;
            this.codeButtonText = '发送验证码';
            this.codeButtonDisabled = false;
            this.modalService.success({
              nzTitle: '认证提示',
              nzContent: this.authMode === 1 ? '手机号码认证成功' : '电子邮箱认证成功'
            });
          }
        });
      }
      else if (!this.code) {
        this.modalService.info({
          nzTitle: '注册提示',
          nzContent: '请先发送验证码！'
        });
      } else {
        this.modalService.info({
          nzTitle: '注册提示',
          nzContent: '验证码输入错误！'
        });
      }
    }
  }

  close() {
    this.isAuthShow = false;
  }

  getProfile() {
    this.customService.profile().subscribe(result => {
      this.data = result['data'];
      this.securityScore = 30;
      if (this.data['isEmailAuth'])
        this.securityScore += 23;
      if (this.data['isPhoneAuth'])
        this.securityScore += 23;
      if (this.data['authentication'] === 100)
        this.securityScore += 24;
      if (this.securityScore >= 100)
        this.securityGrade = '高级';
      else if (this.securityScore >= 75)
        this.securityGrade = '较高';
      else if (this.securityScore >= 53)
        this.securityGrade = '中等';
      else
        this.securityGrade = '较低';
    });
  }

  constructor(
    private customService: CustomService,
    private modalService: NzModalService
  ) { }

  ngOnInit(): void {
    this.getProfile();
  }

}
